
# 写一个函数 MyRange(start, stop, step), 此函数返回一个列表， 列表内的数据是符合range生成的规则
# L = MyRange(1, 10, 2)  # L = [1, 3, 5, 7, 9]
# for x in MyRange(5):
#     print(x)  # 0  1  2  3  4
#


# 写一个函数
# MyRange(start, stop, step), 此函数返回一个列表， 列表内的数据是符合range生成的规则
def MyRange(*args):
    if len(args) <= 0 or len(args) > 3:
        raise TypeError('参数个数不在1～3范围内')
    if len(args) == 1:
        start = 0
        stop = args[0]
        step = 1
    elif len(args) == 2:
        start = args[0]
        stop = args[1]
        step = 1
    elif len(args) == 3:
        start = args[0]
        stop = args[1]
        step = args[2]
    result = []  # 用于保存结果
    # 生成一些数放入result列表
    cur_number = start
    # 如果 步长为正数
    if step > 0:
        while cur_number < stop:
            result.append(cur_number)
            cur_number += step
    elif step < 0:
        while cur_number > stop:
            result.append(cur_number)
            cur_number += step  # 此时的step小于零

    return result
    # 投机取巧的方法1
    # return list(range(*args))

L = MyRange(1, 10, 2)  # L = [1, 3, 5, 7, 9]
print("L:", L)
for x in MyRange(5):
    print(x)  # 0  1  2  3  4

L2 = MyRange(10, 6, -2)
print("L2:", L2)
